package com.woniuxy.joylifecenter.service.impl;

import com.netflix.discovery.converters.Auto;
import com.woniuxy.common.entity.CenterLog;
import com.woniuxy.common.entity.CenterLogExample;
import com.woniuxy.common.mapper.CenterLogMapper;
import com.woniuxy.joylifecenter.exception.CenterException;
import com.woniuxy.joylifecenter.service.LogService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class LogServiceImpl implements LogService {
    @Autowired
    CenterLogMapper centerLogMapper;


    /**
     * @param logId
     * @return int
     * @Author ArmWatt
     * @Date 2020/5/1
     * @Param [logId]
     * @Description 手动批量删除日志
     */
    @Override
    public int batchDeleteLogManually(List<String> logId) {
        CenterLogExample example = new CenterLogExample();
        CenterLogExample.Criteria criteria = example.createCriteria();
        criteria.andCenterLogidIn(logId);
        return centerLogMapper.deleteByExample(example);

    }

    /**
     * @param logId
     * @return void
     * @Author ArmWatt
     * @Date 2020/5/1
     * @Param [logId]
     * @Description 手动删除单条日志
     */
    @Override
    public int deleteLogManually(String logId) {
        return centerLogMapper.deleteByPrimaryKey(logId);
    }

    /**
     * @return java.util.List<com.woniuxy.common.entity.CenterLog>
     * @Author ArmWatt
     * @Date 2020/4/30
     * @Param []
     * @Description 查询全部日志
     **/
    @Override
    public List<CenterLog> queryAllLog() {
        CenterLogExample example = new CenterLogExample();
        CenterLogExample.Criteria criteria = example.createCriteria();
        criteria.andCenterLogidIsNotNull();
        example.setOrderByClause("'centerCreatetime' DESC");

        return centerLogMapper.selectByExample(example);


    }

    /**
     * @return java.util.List<com.woniuxy.common.entity.CenterLog>
     * @Author ArmWatt
     * @Date 2020/4/30
     * @Param [centerLog]
     * @Description 多条件模糊查询日志记录
     **/
    @Override
    public List<CenterLog> queryLogByCondition(CenterLog centerLog) {
        CenterLogExample example = new CenterLogExample();
        CenterLogExample.Criteria criteria = example.createCriteria();
        //查询日志ID
        if (centerLog.getCenterLogid() != null) {
            criteria.andCenterLogidLike("%" + centerLog.getCenterLogid() + "%");
        }
        //查询用户名
        if (centerLog.getCenterUsername() != null) {
            criteria.andCenterUsernameLike("%" + centerLog.getCenterUsername() + "%");
        }
        //查询操作关键字
        if (centerLog.getCenterOperation() != null) {
            criteria.andCenterOperationLike("%" + centerLog.getCenterOperation() + "%");
        }
        //查询IP
        if (centerLog.getCenterIp() != null) {
            criteria.andCenterIpLike("%" + centerLog.getCenterIp() + "%");
        }
        //查询晚于指定时间的记录
        if (centerLog.getCenterCreatetime() != null) {
            criteria.andCenterCreatetimeGreaterThanOrEqualTo(centerLog.getCenterCreatetime());

        }
        example.setOrderByClause("'centerCreatetime' DESC");
        return centerLogMapper.selectByExample(example);


    }

    /**
     * @return void
     * @Author ArmWatt
     * @Date 2020/4/29
     * @Param [centerLog]
     * @Description 记录日志
     **/
    @Override
    public void saveLog(CenterLog centerLog) {
        centerLogMapper.insertSelective(centerLog);
    }

    /**
     * @return int
     * @Author ArmWatt
     * @Date 2020/4/29
     * @Param [createTime]
     * @Description 定时删除日志
     **/
    @Override
    public int deleteLogBySchedule(Date createTime) {

        return centerLogMapper.deleteLogByDate(createTime);
    }
}
